package com.zckj.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;

public class JCDataUtil {
    private static Logger log  = Logger.getLogger(JCDataUtil.class);

    /**
     * 集创传感器状态指令实时数据处理
     * @param b
     */
    public static JSONObject parseJCData(byte[] b) {

        //创建字符操作接收设备编号
        StringBuilder cNo1 = new StringBuilder();
        try {
            //设备编号占8个字节，从数据协议可知设备编号从第16个字节开始，用for循环依次取出8个字节得到设备编号
            for (int i = 23; i > 15; i--) {
                //java.lang.Integer.toHexString() 方法的参数是int(32位)类型，如果输入一个byte(8位)类型的数字，这个方法会把这个数字的高24为也看作有效位，这就必然导致错误，使用& 0XFF操作，可以把高24位置0以避免这样错误的发生。
                String str = Integer.toHexString(b[i] & 0xff);
                if (str.length() < 2) {
                    cNo1.append(0);
                }
                cNo1.append(str);
            }


            //转化得到设备编号
            String cNo = String.valueOf(Long.parseUnsignedLong(cNo1.toString(), 16));

            //获取网络状态
            int onLine = 0;//默认为0为有线网络
            int onLineTemp = Integer.parseInt(Integer.toHexString(b[5]& 0xff)+Integer.toHexString(b[6] & 0xff));
            if (onLineTemp == 6){//6为无线网络
                onLine = 1;
            }
            //获取第一路电压
            int v1 = Integer.parseInt(Integer.toHexString(b[26] & 0xff) + Integer.toHexString(b[27] & 0xff));

            //获取第一路电流
            double i1 = Double.parseDouble(Integer.toHexString(b[28] & 0xff) + Integer.toHexString(b[29] & 0xff)) / 10;

            //获取第二路电压
            int v2 = Integer.parseInt(Integer.toHexString(b[30] & 0xff) + Integer.toHexString(b[31] & 0xff));

            //获取第二路电流
            double i2 = Double.parseDouble(Integer.toHexString(b[32] & 0xff) + Integer.toHexString(b[33] & 0xff)) / 10;

            //获取第三路电压
            int v3 = Integer.parseInt(Integer.toHexString(b[34] & 0xff) + Integer.toHexString(b[35] & 0xff));

            //获取第三路电流
            double i3 = Double.parseDouble(Integer.toHexString(b[36] & 0xff) + Integer.toHexString(b[37] & 0xff)) / 10;

            //获取第四路电压
            int v4 = Integer.parseInt(Integer.toHexString(b[38] & 0xff) + Integer.toHexString(b[39] & 0xff));

            //获取第四路电流
            double i4 = Double.parseDouble(Integer.toHexString(b[40] & 0xff) + Integer.toHexString(b[41] & 0xff)) / 10;

            //第一路温度  默认为0
            double tempIn = 0;

            if (!(Integer.toHexString(b[42] & 0xff) + Integer.toHexString(b[43] & 0xff)).equals("8000")) {
                if (Integer.toHexString(b[42] & 0xff).startsWith("8"))
                    tempIn = -Double.parseDouble(Integer.toHexString(b[42] & 0x0f) + Integer.toHexString(b[43] & 0xff))
                            / 10;
                //避免-0.0的情况出现
                String temp = ""+tempIn;
                if("-0.0".equals(temp.trim())){
                    tempIn = 0.0;
                }
                else
                    tempIn = Double.parseDouble(Integer.toHexString(b[42] & 0x0f) + Integer.toHexString(b[43] & 0xff))
                            / 10;
            }

            //第一路湿度  默认为0
            double humIn = 0;

            if (!(Integer.toHexString(b[44] & 0xff) + Integer.toHexString(b[45] & 0xff)).equals("8000")) {
                humIn = Double.parseDouble(Integer.toHexString(b[44] & 0x0f) + Integer.toHexString(b[45] & 0xff)) / 10;
            }

            //第二路温度  默认为0
            double tempOut = 0;

            if (!(Integer.toHexString(b[46] & 0xff) + Integer.toHexString(b[47] & 0xff)).equals("8000")) {
                if (Integer.toHexString(b[46] & 0xff).startsWith("8"))
                    tempOut = -Double.parseDouble(Integer.toHexString(b[46] & 0x0f) + Integer.toHexString(b[47] & 0xff))
                            / 10;
                //避免-0.0的情况出现
                String temp = ""+tempOut;
                if("-0.0".equals(temp.trim())){
                    tempOut = 0.0;
                }
                else
                    tempOut = Double.parseDouble(Integer.toHexString(b[46] & 0x0f) + Integer.toHexString(b[47] & 0xff))
                            / 10;
            }

            //第二路湿度  默认为0
            double humOut = 0;

            if (!(Integer.toHexString(b[48] & 0xff) + Integer.toHexString(b[49] & 0xff)).equals("8000")) {
                humOut = Double.parseDouble(Integer.toHexString(b[48] & 0x0f) + Integer.toHexString(b[49] & 0xff)) / 10;
            }

            //数据构建
            JSONObject object = new JSONObject();

            object.put("type", "CGQ");
            object.put("cNo", cNo);
            object.put("tempIn", tempIn);
            object.put("tempOut", tempOut);
            object.put("humIn", humIn);
            object.put("humOut", humOut);
            object.put("i1", i1);
            object.put("i2", i2);
            object.put("i3", i3);
            object.put("i4", i4);
            object.put("v1", v1);
            object.put("v2", v2);
            object.put("v3", v3);
            object.put("v4", v4);
            object.put("online",onLine);
            return object;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }



    /**
     * 集创机柜检测按钮状态指令数据分析
     */
    public static JSONObject parseDoorStateTwo(byte[] b) {

        StringBuilder cNo1 = new StringBuilder();
        try {
            for (int i = 23; i > 15; i--) {
                String str = Integer.toHexString(b[i] & 0xff);
                if (str.length() < 2) {
                    cNo1.append(0);
                }
                cNo1.append(str);
            }

            //设备编号
            String cNo = String.valueOf(Long.parseUnsignedLong(cNo1.toString(), 16));

            //门状态 默认为0
            String door = "0";// 检修
            if ((b[26] & 0xff) == 170)
                door = "1";// 非法开门,也是开门状态
            Object[] arr = { door, cNo };
            //更新机柜信息
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("type", "BUTTON");
            jsonObject.put("cNo", cNo);
            jsonObject.put("door", door);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 门状态数据处理
     * @param b
     */
    public static JSONObject parseDoorStateOne(byte[] b) {
        StringBuilder cNo1 = new StringBuilder();
        try {
            for (int i = 23; i > 15; i--) {
                String str = Integer.toHexString(b[i] & 0xff);
                if (str.length() < 2) {
                    cNo1.append(0);
                }
                cNo1.append(str);
            }
            //设备编号
            String cNo = String.valueOf(Long.parseUnsignedLong(cNo1.toString(), 16));

            //门状态
            String door1 = "0";// 关门
            String door2 = "0";// 关门
            if ((b[26] & 0xff) == 170)
                door1 = "1";// 开门
            if ((b[27] & 0xff) == 170)
                door2 = "1";// 开门
            //数据数组
            Object[] arr = { cNo };

            JSONObject object = new JSONObject();
            object.put("type", "DOOR");
            object.put("cNo", cNo);
            object.put("door1", door1);
            object.put("door2", door2);

            return object;//返回解析的采集数据

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


}
